
function Clearnav() {
	var pp = $('#wnav').accordion('panels');
	$.each(pp, function(i, n) {
		if (n) {
			var t = n.panel('options').title;
			$('#wnav').accordion('remove', t);
		}
	});

	pp = $('#wnav').accordion('getSelected');
	if (pp) {
		var title = pp.panel('options').title;
		$('#wnav').accordion('remove', title);
	}
}

function addNav(data) {
	$.each(data, function(i, sm) {
		var menulist = "";
		menulist += "<ul class='nav-tree'>";
		$.each(sm.child_menu, function(j, o) {
			menulist += '<li><div><a ref="' + o.id + '" href="#" rel='
					+ o.menu_path + '" >&nbsp;&nbsp;<span class="fa fa-' + o.menu_icon
					+ '" >&nbsp;&nbsp;&nbsp;</span><span class="nav">' + o.menu_name
					+ '</span></a></div></li> ';
		});
		menulist += '</ul>';

		$('#wnav').accordion('add', {
			title : sm.menu_name,
			content : menulist,
			iconCls : 'fa fa-'+sm.menu_icon+' fa-lg fa-lg',
			height : '25px'
		});
	});
	//二级菜单高度自适应
	$('#wnav div.accordion-body').css('height','auto');
	var pp = $('#wnav').accordion('panels');
	var t = pp[0].panel('options').title;
	$('#wnav').accordion('select', t);
	
	var pp = $('#wnav').accordion('panels');
	var t = pp[0].panel('options').title;
	$('#wnav').accordion('select', t);
}

function createFrame(url) {
	var s = '<iframe scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>';
	return s;
}

function tabClose() {
	/* 双击关闭TAB选项卡 */
	$(".tabs-inner").dblclick(function() {
		var subtitle = $(this).children(".tabs-closable").text();
		$('#tabs').tabs('close', subtitle);
	});
	/* 为选项卡绑定右键 */
	$(".tabs-inner").bind('contextmenu', function(e) {
		$('#mm').menu('show', {
			left : e.pageX,
			top : e.pageY
		});
		var subtitle = $(this).children(".tabs-closable").text();
		$('#mm').data("currtab", subtitle);
		$('#tabs').tabs('select', subtitle);
		return false;
	});
}

function addTab(subtitle, url) {
	if (!$('#tabs').tabs('exists', subtitle)) {
		$('#tabs').tabs('add', {
			title : subtitle,
			content : createFrame(url),
			closable : true,
		});
	} else {
		$('#tabs').tabs('select', subtitle);
		$('#mm-tabupdate').click();
	}
	tabClose();
}

// 初始化左侧
function initMenu() {
	//菜单项鼠标Hover
	$('.nav-tree a').hover(function() {
		$(this).parent().addClass("hover");
	}, function() {
		$(this).parent().removeClass("hover");
	});
	$('.nav-tree a').bind("click",function() {
		var tabTitle = $(this).children('.nav').text();
		var url = $(this).attr("rel");
		var menuid = $(this).attr("ref");
		addTab(tabTitle, url);
		$('#wnav li div').removeClass("selected");
		$(this).parent().addClass("selected");
	});
}


function updpwd(){
	$('#updpsw').window('open');
	$('#pswform').attr('action','/d/updpsw');
}

function subpsw(){
	if ($('#newpsw').val() != $('#repsw').val()) {
		$.messager.alert('提示','两次输入的新密码不一致！请重新输入！','warning');
		return;
	}
	$('#pswform').form('submit',{
		onSubmit:function(){
			$('.panel-tool-close').trigger('click');
        	return $(this).form('enableValidation').form('validate');
        },
		success:function(data){
			$('#pswform').form('clear');
			if(0==data)
				$.messager.alert('提示','修改成功！','info');
			else
				$.messager.alert('提示','您输入的原密码有误，请重试！','error');
		}
	});
}

function updself(){
	$.ajax({
		url : '/d/selfinfo',
		type : 'post',
		dataType : 'json',
		success : function(data) {
			$('#selfform').form('load',data);
		},
	});
	$('#updself').dialog('open');
	$('#selfform').attr('action','/d/updself');
}

function subself(){
    $('#selfform').form('submit',{
        onSubmit:function(){
        	$('.panel-tool-close').trigger('click');
            return $(this).form('enableValidation').form('validate');
        },
	    success:function(data){
    		$("#selfform").form("clear");
			if(0==data)
				$.messager.alert('提示','修改成功！','info');
			else
				$.messager.alert('提示','修改失败，请重试！','error');
	    }
    });
}

function logout(){
	$.messager.confirm('提示', '您确定要退出本次登录吗?',
		function(r) {
			if(r)
				window.location.href = "/logout";
		}
	);
}

